
import type { Station } from "@/common/types/interface";
import { queryStations } from "@/common/station_api";
import { defineStore } from "pinia";
export const useRadioSearchStore = defineStore('radioSearchStore', {
	state: () => ({
		//首页查询列表数据
		searchText: '',
		filteredList: [] as Station[],
		page: 1,
		pageSize: 10,
		hasMore: true,
		isLoading: false,
		queryType: '' as 'search' | 'lang' | 'tags',
		queryParams: {} as any
	}),

	actions: {
		async loadMore() {
			if (!this.hasMore || this.isLoading) return;
			this.isLoading = true;
			const res = await queryStations(this.queryType, this.queryParams, this.page, this.pageSize);
			if (res.length < this.pageSize) this.hasMore = false;
			this.filteredList.push(...res);
			this.page++;
			this.isLoading = false;
		},

		async resetQuery(type: 'search' | 'lang' | 'tags', params: any) {
			this.filteredList = [];
			this.page = 1;
			this.hasMore = true;
			this.queryType = type;
			this.queryParams = params;
			if (params?.keyword || params?.lang || params?.tags?.length) await this.loadMore();
		},
		clearResults() {
			this.searchText = '';
			this.filteredList = [];
		}

	},
	unistorage: false
});